﻿using SevenZip;
using StarryEdge.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarryEdge.Domains
{
    public class ZIP7 : ICompressionDecompression
    {
        public ZIP7()
        {
            var path = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "7z.dll");
            SevenZipBase.SetLibraryPath(path);
        }

        public bool Compression(string targetName, params string[] files)
        {
            try
            {
                var tmp = new SevenZipCompressor();
                tmp.ScanOnlyWritable = true;
                tmp.CompressFiles(targetName, files);
                return true;
            }
            catch (Exception)
            { }
            return false;
        }

        public bool Decompression(string targetFile, string targetAddress)
        {
            try
            {
                using (var tmp = new SevenZipExtractor(targetFile))
                {
                    for (int i = 0; i < tmp.ArchiveFileData.Count; i++)
                    {
                        tmp.ExtractFiles(targetAddress, tmp.ArchiveFileData[i].Index);
                    }
                }
            }
            catch (Exception)
            { }
            return false;
        }
    }
}
